package com.acme.bankapp.domain.accounts;

import com.acme.bankapp.domain.Client;
import com.acme.bankapp.exceptions.NegativeAmountException;
import com.acme.bankapp.exceptions.NotEnoughFundsException;

/**
 * Represents an account with NO overdraft.
 * 
 * Holds balance info.
 * Ensures safety of business rules.
 *
 */
public class SavingsAccount extends AbstractAccount {

	/**
	 * Creates a Savings account with zero balance.
	 */
	public SavingsAccount() {
		balance = 0.;
		accountType = AccountType.SAVINGS;
		id = updateNextId();
	}
	
	/**
	 * Creates a Savings account with specified balance.
	 */
	public SavingsAccount(double balance) {
		this();
		this.balance = balance; 
	}

	/**
	 * Withdraws amount >= 0. from account.
	 * {$inheritDoc}
	 * @param amount amount to be withdrawn.
	 * @return true if amount is non-negative and amount is small enough to be withdrawn, false otherwise.
	 */
	public boolean withdraw(double amount) throws NotEnoughFundsException, NegativeAmountException {
		if (amount < 0.) {
			throw new NegativeAmountException();
		}

		if (amount < balance) {
			balance -= amount;
			return true;
		} else {
			throw new NotEnoughFundsException();
		}
	}

	/**
	 * Adds amount > 0. to account's balance.
	 * {$inheritDoc}
	 * @param amount amount to be deposited.
	 * @return true if amount positive, false otherwise.
	 */
	public boolean deposit(double amount) throws NegativeAmountException  {
		if (amount < 0.) {
			throw new NegativeAmountException();
		}
		balance += amount;
		return true;
	}

	public double getBalance() {
		return balance;
	}

	/**
	 * Returns balance as String.
	 */
	@Override
	public String toString() {
		return "" + getBalance();
	}

	@Override
	public long decimalValue() {
		return Math.round(getBalance());
	}
	
}